﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using RaisingStudio.Data.Providers;
using RaisingStudio.Data;
using RaisingStudio.Data.Common;
using RaisingStudio.Data.Entities;

namespace UnitTest
{
    [TestFixture]
    public class ConfigFileTest
    {
        [Test]
        public void Test1()
        {
            // test connections.config.
            DataContext dc = new DataContext("dac");
            Assert.IsNotNull(dc);
            Assert.AreEqual(DatabaseType.OTHER, dc.DatabaseType);
            Assert.AreEqual("sqlServer20005", dc.ProviderName);
            Assert.AreEqual("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"|DataDirectory|Database1.mdf\";Integrated Security=True;User Instance=True", dc.Connection.ConnectionString);
            Assert.AreEqual(".\\SQLEXPRESS", (dc.Connection as System.Data.SqlClient.SqlConnection).DataSource);

            DataContext dc2 = new DataContext("dac2");
            Assert.IsNotNull(dc2);
            Assert.AreEqual(DatabaseType.ORACLE, dc2.DatabaseType);
            Assert.IsTrue(string.IsNullOrEmpty(dc2.ProviderName));
            Assert.AreEqual("Data source=dacs2; User ID=dac_owner; Password=dac_owner", dc2.Connection.ConnectionString);
            Assert.AreEqual("dacs2", (dc2.Connection as System.Data.OracleClient.OracleConnection).DataSource);
        }

        [Test]
        public void Test2()
        {
            // test definitions.config.
            RaisingStudio.Data.Providers.DataContext dc = new DataContext();
            UTIssue issue = new UTIssue();
            issue.CreateTime = System.DateTime.Now;
            issue.Description = "UTIssue for UnitTest.";
            issue.IssueType = IssueType.Issue;
            issue.Locked = false;
            issue.Owner = "sa";
            issue.Progress = 10;
            issue.Status = IssueStatus.New;
            issue.Title = "issue Title";
            int result = dc.Insert<UTIssue>(issue);
            int issueID = Convert.ToInt32(dc.GetMax<UTIssue>("IssueID"));
            Assert.AreEqual(1, result);
            Assert.Greater(issueID, 0);
            result = dc.Delete<UTIssue>(issueID);
            Assert.AreEqual(1, result);

            string userID = System.Guid.NewGuid().ToString();

            UTUser user = new UTUser();
            user.UserID = userID;
            user.UserName = "UTUser name";
            user.FullName = "full name";
            user.ChangingPassword = false;
            user.UserDescription = "user description";

            result = dc.Save<UTUser>(user);
            Assert.AreEqual(1, result);
            result = dc.Delete<UTUser>(user);
            Assert.AreEqual(1, result);
        }

        [Test]
        public void Test3()
        {
            // test providers.config.
            // reference to Test1;
        }

        [Test]
        public void Test4()
        {
            // test commands.config.
            CommonCommand command = CommandManager.Instance.GetCommand("select");
            Assert.IsNotNull(command);
            Assert.AreEqual("SELECT * FROM DAC_ISSUE", command.CommandText);
            Assert.AreEqual(":", command.ParameterPrefix);

            CommonCommand c2 = CommandManager.Instance.GetCommand("select2");
            Assert.IsNotNull(c2);
            Assert.AreEqual("SELECT * FROM DAC_USER", c2.CommandText.Trim());

            CommonCommand c3 = CommandManager.Instance.GetCommand("select3");
            Assert.IsNotNull(c3);
            Assert.AreEqual("SELECT_DAC_ISSUE", c3.CommandText);
            Assert.AreEqual(System.Data.CommandType.StoredProcedure, c3.CommandType);

            CommonCommand c4 = CommandManager.Instance.GetCommand("select4");
            Assert.IsNotNull(c4);
            Assert.AreEqual(@"SELECT * FROM DAC_ISSUE DI
    WHERE DI.ISSUE_ID = :ISSUE_ID", c4.CommandText.Trim());
        }

        [Test]
        public void Test5()
        {
            // test converters.config.
            DataContext dc = new DataContext();
            dc.Log = System.Console.Out;

            RaisingStudio.Data.Entities.Line line = new RaisingStudio.Data.Entities.Line();
            line.Point = new RaisingStudio.Data.Entities.Point(1, 2, 3);
            int result = dc.Insert<RaisingStudio.Data.Entities.Line>(line);
            Assert.AreEqual(1, result);

            int pointID = Convert.ToInt32(dc.GetMax<RaisingStudio.Data.Entities.Line>(RaisingStudio.Data.Entities.Line._.PointID));
            RaisingStudio.Data.Entities.Line line2 = dc.GetData<RaisingStudio.Data.Entities.Line>(pointID);

            Assert.AreEqual(line.ToString(), line2.ToString());

            result = dc.Delete<RaisingStudio.Data.Entities.Line>(pointID);
            Assert.AreEqual(1, result);
        }
    }
}
